﻿using SH.Common.CommonClass;

using SH.Multitenant.Core.Saas;
using SH.Multitenant.Domain.Multitenant;
using SH.Multitenant.Domain.RBAC;
using SH.Multitenant.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SH.Multitenant.Infrastructure.Repository
{
    public class EmpRepository : BaseRepository<Employee>, IEmpRepository
    {
        private readonly ISqlSugarClient _context;
        public EmpRepository(ISqlSugarClient context) : base(DbManger.BizDb)
        {
            _context = DbManger.BizDb;
        }
        //根据用户名获取员工实体
        public async Task<Employee> GetEmployeeModel(int? storeId, string? uName, string uPwd)
        {
            //根据门店找到对应的租户
            var store = await _context.AsTenant().QueryableWithAttr<Store>().FirstAsync(x => x.StoreId == storeId);
            //根据租户切库
            //索引租户的数据库
            var tenant = await _context.AsTenant().QueryableWithAttr<DbOptions>().FirstAsync(x => x.TenantId == store.TenantId);
            _context.AsTenant().AddConnection(new ConnectionConfig()
            {
                ConfigId = store.TenantId,
                ConnectionString = tenant.Conn,
                DbType = DbType.MySql,
            });
            var db = _context.AsTenant().GetConnection(store.TenantId);
            //查询租户库下的员工表
            var emp = await db.Queryable<Employee>().FirstAsync(x => x.EmpName == uName && x.StoreId == storeId);
            return emp;
        }


    }
}
